package com.cjy.housePosting.action;

import com.base.common.ConfigConstants;
import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.enums.HouseRequestStatus;
import com.base.common.persistent.model.HouseInfo;
import com.base.common.persistent.model.HousePostingDetails;
import com.base.common.persistent.model.HouseRequest;
import com.base.common.utils.IDGenerator;
import com.cjy.common.bean.Files;
import com.cjy.common.bean.OpAttributes;
import com.cjy.housePosting.service.HousePostingService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.dispatcher.StrutsRequestWrapper;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;

import static com.base.GlobaConstants.*;

@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class HousePostingAction extends AbstractBaseAction{

	private static final long serialVersionUID = 1L;

	@Resource
	private HousePostingService housePostingService;

	/* Struts2文件上传的三个属性 */
	private File[] filePhotos;
	private String[] filePhotosContentType;
	private String[] filePhotosFileName;

	private String title;					// 标题
	private String description;				// 描述
	private String contact;					// 联系人
	private String contactPhone;			// 联系人电话
	private int contactIdentity;			// 来源，0 个人，1 经纪人
	private int categories;					// 记录类别。0，租房，1，售房
	private int publisher;					// 发布人id，即当前设备主键id
	private int status;						// 发布状态
	
	private String part;					// 区域，即省市区
	private String detailedAddress;			// 详细地址
	private int price;						// 价格，以元为单位
	private int area;						// 房屋面积，以平米为单位
	private String floor;					// 楼层，例如：第XX层/共XX层
	private String orientation;				// 朝向
	private String decoration;				// 装修
	private float age;						// 房龄
	private String type;					// 户型
	private String rentalMode;				// 租赁模式 
	private String rentPaymentMethod;		// 租金支付方式
	private String supportingFacilities;		// 配套设施

	
	private Date orderTime;					// 预约时间
	private int housePostingDetailsId;		// 房屋发布记录ID
	
	
	private String code;					// 设备码
	private int bid;						// 设备标识符
	
	private int page; 	// 当前页数

	public File[] getFilePhotos() {
		return filePhotos;
	}

	public void setFilePhotos(File[] filePhotos) {
		this.filePhotos = filePhotos;
	}

	public String[] getFilePhotosContentType() {
		return filePhotosContentType;
	}

	public void setFilePhotosContentType(String[] filePhotosContentType) {
		this.filePhotosContentType = filePhotosContentType;
	}

	public String[] getFilePhotosFileName() {
		return filePhotosFileName;
	}

	public void setFilePhotosFileName(String[] filePhotosFileName) {
		this.filePhotosFileName = filePhotosFileName;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	public String getContactPhone() {
		return contactPhone;
	}

	public void setContactPhone(String contactPhone) {
		this.contactPhone = contactPhone;
	}

	public int getContactIdentity() {
		return contactIdentity;
	}

	public void setContactIdentity(int contactIdentity) {
		this.contactIdentity = contactIdentity;
	}

	public int getCategories() {
		return categories;
	}

	public void setCategories(int categories) {
		this.categories = categories;
	}

	public int getPublisher() {
		return publisher;
	}

	public void setPublisher(int publisher) {
		this.publisher = publisher;
	}

	public String getPart() {
		return part;
	}

	public void setPart(String part) {
		this.part = part;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public int getBid() {
		return bid;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	public String getDetailedAddress() {
		return detailedAddress;
	}

	public void setDetailedAddress(String detailedAddress) {
		this.detailedAddress = detailedAddress;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public int getArea() {
		return area;
	}

	public void setArea(int area) {
		this.area = area;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getFloor() {
		return floor;
	}

	public void setFloor(String floor) {
		this.floor = floor;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getOrientation() {
		return orientation;
	}

	public void setOrientation(String orientation) {
		this.orientation = orientation;
	}

	public String getDecoration() {
		return decoration;
	}

	public void setDecoration(String decoration) {
		this.decoration = decoration;
	}

	public float getAge() {
		return age;
	}

	public void setAge(float age) {
		this.age = age;
	}
	
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getRentalMode() {
		return rentalMode;
	}

	public void setRentalMode(String rentalMode) {
		this.rentalMode = rentalMode;
	}

	public String getRentPaymentMethod() {
		return rentPaymentMethod;
	}

	public void setRentPaymentMethod(String rentPaymentMethod) {
		this.rentPaymentMethod = rentPaymentMethod;
	}

	public String getSupportingFacilities() {
		return supportingFacilities;
	}

	public void setSupportingFacilities(String supportingFacilities) {
		this.supportingFacilities = supportingFacilities;
	}

	public Date getOrderTime() {
		return orderTime;
	}

	public void setOrderTime(Date orderTime) {
		this.orderTime = orderTime;
	}

	/**
	 * @return the housePostingDetailsId
	 */
	public int getHousePostingDetailsId() {
		return housePostingDetailsId;
	}

	/**
	 * @param housePostingDetailsId the housePostingDetailsId to set
	 */
	public void setHousePostingDetailsId(int housePostingDetailsId) {
		this.housePostingDetailsId = housePostingDetailsId;
	}

	/**
	 * 添加房屋发布记录
	 * 
	 * @return
	 */
	@Action(value = "addHousePosting", interceptorRefs = { @InterceptorRef("fileUploadInterceptor") })
	public String addHousePosting() {
		System.out.println("进入addHousePostingAction..........");
		try {
			HouseInfo houseInfo = new HouseInfo();
			houseInfo.setAge(age);
			houseInfo.setArea(area);
			houseInfo.setCode(IDGenerator.getUUID());
			houseInfo.setDecoration(decoration);
			houseInfo.setDetailedAddress(detailedAddress);
			houseInfo.setFloor(floor);
			houseInfo.setOrientation(orientation);
			houseInfo.setPart(part);
			houseInfo.setPrice(price);
			houseInfo.setType(type);
			houseInfo.setRentalMode(rentalMode);
			houseInfo.setRentPaymentMethod(rentPaymentMethod);
			houseInfo.setSupportingFacilities(supportingFacilities);
			
			HousePostingDetails housePostingDetails = new HousePostingDetails();
			housePostingDetails.setHouseInfo(houseInfo);
			housePostingDetails.setCategories(categories);
			housePostingDetails.setContact(contact);
			housePostingDetails.setContactIdentity(contactIdentity);
			housePostingDetails.setContactPhone(contactPhone);
			housePostingDetails.setCreateTime(new Date());
			housePostingDetails.setDescription(description);
			housePostingDetails.setTitle(title);
			
			Files files = new Files();
			files.setFile(filePhotos);
			files.setFileContentType(filePhotosContentType);
			files.setFileFileName(filePhotosFileName);
			
			// 因为是上传文件，用了common-upload的拦截器处理之后request会报空，要得到一般请求的request头，就要对request做个转型，才能调用相应方法
			HttpServletRequest servletRequest = new StrutsRequestWrapper(request);
			String basePath = null;
			String path = servletRequest.getContextPath();
			basePath = servletRequest.getScheme() + "://"
					+ ConfigConstants.SERVERIP + ":"
					+ servletRequest.getServerPort() + path + "/";
			
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", housePostingService.addHousePosting(housePostingDetails, files, basePath, code, bid)));
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
			e.printStackTrace();
		}
		return returnType();
	}
	
	/**
	 * 根据发布类别查询发布记录条数
	 * 
	 * @return
	 */
	@Action(value = "queryHousePostingCounts")
	public String queryHousePostingCounts() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result", new OpAttributes().add("totalCounts", housePostingService.queryHousePostingCounts(categories, status, code, bid))));
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
			e.printStackTrace();
		}
		return returnType();
	}
	
	/**
	 * 分页查询发布记录列表
	 * 
	 * @return
	 */
	@Action(value = "queryHousePosingList")
	public String queryHousePosingList() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result", housePostingService.queryHousePosingList(page, categories, status, code, bid)));
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
			e.printStackTrace();
		}
		return returnType();
	}
	
	/**
	 * 增加看房记录
	 * 
	 * @return
	 */
	@Action(value = "addHouseRequest")
	public String addHouseRequest() {
		try {
			HouseRequest houseRequest = new HouseRequest();
			houseRequest.setContact(contact);
			houseRequest.setContactPhone(contactPhone);
			houseRequest.setDescription(description);
			houseRequest.setStatus(HouseRequestStatus.PROCESSING.getValue());
			houseRequest.setCreateTime(new Date());
			houseRequest.setOrderTime(orderTime);
			
			HousePostingDetails details = new HousePostingDetails();
			details.setId(housePostingDetailsId);
			houseRequest.setHousePostingDetails(details);
			
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", housePostingService.addHouseRequest(houseRequest, code, bid)));
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
			e.printStackTrace();
		}
		return returnType();
	} 
}
